package com.finalproject.entity;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * Representation entity class of CardHolder
 * @author Cagri Akargun 
 * */
public class CardHolder {
	//private String name;
	private int id;
	//unique two digit code 
	//private int familyId;
	private int pincode;
	private Map<Integer, String> nameMap;
	private Map<Integer, DietaryProfile> dietaryMap;
	private ExpenseProfile expense;
	private DietaryProfile tempDietary;
	private String tempName;
	
	public CardHolder() {
		
	}
	
	//I'll get rid of this constructor later!!!!!!!!!!!!!!!!!!!!!!!!
	//
	public CardHolder(int id, int familyId, String name, int pinCode, ExpenseProfile expenseProfile, DietaryProfile dietaryProfile) {
		this.id = id;
		//this.name = name;
		this.pincode = pinCode;
		this.expense = expenseProfile;
		this.tempDietary = dietaryProfile;
	}
	
	
	//CardHolder doeFamily=new CardHolder(123456, 1234, ["John Doe", "Jane Doe"],[00, 01]);
	//family id is 0 for normal users, 1-99 for multiusers
	/**
	 * Constructor for cardholder
	 * @param id the assigned id number
	 * @param pinCode cardholder's pincode
	 * */
	public CardHolder(int id, int pinCode) {
		this.id=id;
		nameMap= new HashMap<Integer, String>();
		this.pincode = pinCode;
		expense=new ExpenseProfile();
		dietaryMap = new HashMap<Integer,DietaryProfile>();
		
	}
	public void addMember(String name, int familyId){
		nameMap.put(Integer.valueOf(familyId), name);
		dietaryMap.put(Integer.valueOf(familyId), new DietaryProfile());
	}
	
	public void selectCurrentMember(int familyId){
		if (familyId<0 || familyId>99)
			throw new IllegalArgumentException("the family id must be a two digit number");
		tempDietary=dietaryMap.remove(Integer.valueOf(familyId));
		tempName=nameMap.remove(Integer.valueOf(familyId));
	}
	
	public void buyItem(SnackItem item) throws CaloriesExceededException, InsufficientFundsException{
		expense.removeFunds(item.getPrice());
		tempDietary.buyFood(item);
		
		
	}
	
	public void buyItem(SnackItem item, Calendar date) throws InsufficientFundsException{
		tempDietary.buyFood(item, date);
		expense.removeFunds(item.getPrice(), date);
	}
	public void addFunds(double amount){
		expense.addFunds(amount);
	}
	public void logout(int familyId){
		dietaryMap.put(Integer.valueOf(familyId), tempDietary);
		nameMap.put(Integer.valueOf(familyId), tempName);
		
	}
	
	public String getName() {
		return tempName;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	public int getPincode() {
		return pincode;
	}
	public void setPincode(int pincode) {
		this.pincode = pincode;
	}
	public DietaryProfile getDietary() {
		return tempDietary;
	}
	
	public ExpenseProfile getExpense() {
		return expense;
	}
	
	public Map<Integer,String> getNameMap() {
		return nameMap;
	}
	
	
	
}
